package com.wangct.zzzpro;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.Toast;

public class MainActivity extends Activity implements OnItemClickListener {
	private MyGridView gridview;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initView();
	}

	private void initView() {

		// 设置九宫格的gridview
		gridview = (MyGridView) findViewById(R.id.gridview);
		gridview.setHorizontalSpacing(0);
		gridview.setNumColumns(3);
		gridview.setScrollBarStyle(View.LAYER_TYPE_NONE);// ,
		gridview.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);
		gridview.setVerticalSpacing(0);
		//  string数组
		String[] imgText = { "转账", "余额宝", "手机充值", "信用卡还款", "淘宝电影", "彩票", "当面付", "亲密付", "机票" };
		// int图片地址数组
		int[] imges = { R.drawable.app_transfer, R.drawable.app_fund, R.drawable.app_phonecharge,
				R.drawable.app_creditcard, R.drawable.app_movie, R.drawable.app_lottery, R.drawable.app_facepay,
				R.drawable.app_close, R.drawable.app_plane };
		gridview.setAdapter(new MyGridAdapter(this, imgText, imges) {
		});
		gridview.setOnItemClickListener(this);

	}

	// 这里处理webview的跳转
	private void loadwebview(String url) {
		// 打开浏览器页面
		Intent intentBro = new Intent();
		intentBro.putExtra("url", "" + url);
		intentBro.setClass(getApplicationContext(), MyWebViewAct.class);
		startActivity(intentBro);
	}

	private void showmsg(String s) {
		Toast.makeText(this, "" + s, 0).show();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub

		if (keyCode == event.KEYCODE_BACK) {

			exitBy2Click();
			return true;// 注意return
		}
		return super.onKeyDown(keyCode, event);
	}

	/**
	 * 双击退出函数
	 */
	private static Boolean isExit = false;

	private void exitBy2Click() {
		Timer tExit = null;
		if (isExit == false) {
			isExit = true; // 准备退出
			Toast.makeText(this, "再按一次关闭", Toast.LENGTH_SHORT).show();
			tExit = new Timer();
			tExit.schedule(new TimerTask() {
				@Override
				public void run() {
					isExit = false; // 取消退出
				}
			}, 2000); // 如果2秒钟内没有按下返回键，则启动定时器取消掉刚才执行的任务

		} else {
			this.finish();

		}
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int id, long arg3) {
		// TODO Auto-generated method stub
		// showmsg(" int 的值是：" + id + " long的值是：" + arg3 + "");
		Log.d("MainActivity----->", " int 的值是：" + id + "    long的值是：" + arg3);

		loadwebview("https://www.baidu.com");

		switch (id) {
		case 0:

			break;
		case 1:

			break;
		case 2:

			break;
		case 3:

			break;
		case 4:

			break;
		case 5:

			break;
		case 6:

			break;
		case 7:

			break;
		case 8:

			break;

		default:
			break;
		}
	}

}
